﻿using AJWPFAdmin.Core.Components.AJTable.ViewModels;
using AJWPFAdmin.Core.Enums;
using AJWPFAdmin.Core.Utils;
using AJWPFAdmin.Services.EF;
using MaterialDesignExtensions.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static AJWPFAdmin.Core.ExceptionTool;
using System.Windows.Controls;
using Masuit.Tools.Models;
using EntranceGuardManager.Modules.Main.Views;
using AJWPFAdmin.Core.Models.EF.Tables;

namespace EntranceGuardManager.Modules.Main.ViewModels
{
    public class BlacklistedCarListViewModel : BindableBase, INavigationAware
    {
        private AJTableFetchOptions _fetchOptions;

        public AJTableFetchOptions FetchOptions
        {
            get
            {
                return _fetchOptions;
            }
            set
            {
                SetProperty(ref _fetchOptions, value);
            }
        }

        private List<AJTableColumnItem> _columns;

        public List<AJTableColumnItem> Columns
        {
            get { return _columns; }
            set { SetProperty(ref _columns, value); }
        }

        private AJTableSearchFormConfig _formConfig;

        public AJTableSearchFormConfig FormConfig
        {
            get { return _formConfig; }
            set { SetProperty(ref _formConfig, value); }
        }

        private DelegateCommand<DataGridRow> _loadingRowCmd;
        public DelegateCommand<DataGridRow> LoadingRowCmd =>
            _loadingRowCmd ?? (_loadingRowCmd = new DelegateCommand<DataGridRow>(ExecuteLoadingRowCmd));

        void ExecuteLoadingRowCmd(DataGridRow row)
        {

        }

        private IDialogService _dialogSvc;
        private DbService db;

        public BlacklistedCarListViewModel(DbService dbIns, IEventAggregator eventAggregator, IDialogService dialogSvc)
        {
            _dialogSvc = dialogSvc;
            //eventAggregator.GetEvent<AJGlobalExceptionResolvedEvent>().Subscribe(() => Loading = false);

            FetchOptions = new AJTableFetchOptions
            {
                //ExportFileName = "车辆记录导出",
                Handle = async (formCfg, pageCfg) =>
                {
                    var name = FormConfig.Schemas
                        .FirstOrDefault(p => p.Field == nameof(BlacklistedCar.CarNo)).Value?.ToString();

                    var dates = FormConfig.Schemas
                        .FirstOrDefault(p => p.Field == nameof(BlacklistedCar.CreateDate)).Value as ObservableCollection<DateTime?>;

                    var list = await db.BlacklistedCars
                        .BetweenInDates(dates.ToArray(), p => p.CreateDate)
                        .LikeOrLike(name, p => p.CarNo)
                        .OrderByDescending(p => p.CreateDate)
                        .ToPagedListAsync(pageCfg.Current, pageCfg.PageSize);

                    return new AJTableFetchResult
                    {
                        Total = list.TotalCount,
                        HasNext = list.HasNext,
                        Rows = list.Data
                    };
                }
            };

            FormConfig = new AJTableSearchFormConfig
            {
                Schemas = new ObservableCollection<AJTableSearchFormSchema>
                {
                    new AJTableSearchFormSchema
                    {
                        Label = "车牌号",
                        Field = nameof(BlacklistedCar.CarNo),
                        Type = AJTableSchemaType.Input,
                        IsPopular = true,
                    },
                    new AJTableSearchFormSchema
                    {
                        Labels = new string[]{ "开始日期","结束日期" },
                        Field = nameof(AlarmInfoRecord.CreateDate),
                        Type = AJTableSchemaType.RangePicker,
                        Value = new ObservableCollection<DateTime?>( new DateTime?[2]{null,null}),
                        IsPopular = true,
                    },
                },
                AdvFilterVisibility = System.Windows.Visibility.Collapsed,
                ExportVisibility = System.Windows.Visibility.Collapsed,
            };

            Columns = new List<AJTableColumnItem>
            {
                //new AJTableColumnItem
                //{
                //    Title = "操作",
                //    CustomTemplate = new AJTableCustomTemplate
                //    {
                //        Key = "TableOperatioin"
                //    },
                //    Fixed = AJTableColumnFixedType.Right,
                //    Width = new DataGridLength(160),
                //},
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.CarNo),
                    Title = "车牌号"
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.PlateColor),
                    Title = "车牌颜色"
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.VIN),
                    Title = "VIN",
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.FuelTypeName),
                    Title = "燃料类型",
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.EmissionStandard),
                    Title = "排放阶段",
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.Reason),
                    Title = "拉黑原因",
                },
                new AJTableColumnItem
                {
                    DataIndex = nameof(BlacklistedCar.CreateDate),
                    Title = "创建日期",
                    Formatter = new AJTableColumnFormatter
                    {
                        Handler = (val) =>
                        {
                            return ((DateTime)val).ToString("yyyy-MM-dd HH:mm:ss");
                        }
                    }
                }
            };
            db = dbIns;
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //ExecuteGetListCmd();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
    }
}
